package com.market.cash.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.market.cash.R
import com.market.cash.databinding.CashGuideDialogBinding
import com.market.cash.pro.ProDialog
import com.market.cash.utils.CashRootDensityUtil
import com.market.cash.utils.event.CashRootEventHelper
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.thread.TimingThread

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/31 15:41
 * @Annotation：指引dialog
 */
class CashRootGuideDialog: ProDialog, TimingThread.ITimingThreadListener {
    private lateinit var binding: CashGuideDialogBinding
    private var mCallback: IDialogCallback? = null
    private var guideY = 0f

    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 5


    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.MATCH_PARENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window!!.setGravity(Gravity.CENTER)
    }

    constructor(context: Context,setY: Float) : super(context) {
        guideY = setY
    }
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context,setY: Float, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        guideY = setY
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = CashGuideDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        binding.guideLayout.y = guideY
        binding.guideLayout.x += CashRootDensityUtil.dp2px(8f)
        startTimeThread()
    }

    override fun initListener() {
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
    }

    interface IDialogCallback {
        fun onGuideDismiss()
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        //随机 5秒 等待时长
        countdown = 5
        showGuideTipsText()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 1){
            countdown = 5
            timeThread!!.stopThread()

            dismiss()
            if(mCallback != null){
                mCallback!!.onGuideDismiss()
            }
            return
        }
        countdown--
        showGuideTipsText()
    }

    @SuppressLint("SetTextI18n")
    private fun showGuideTipsText(){
        binding.textCountdown.text = "${countdown}s"
    }
    override fun onStart() {
        super.onStart()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_GUIDE_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_GUIDE_DISMISS)
    }
}